﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace TooltracePC
{
    public partial class frmSyncData : Form
    {
        dgv dgvObject = new dgv();
        private static int _fileStatus = 0;
        private static string _title = "";

        public frmSyncData()
        {
            InitializeComponent();
        }

        private void frmSyncData_Load(object sender, EventArgs e)
        {
            string fileName = dgvObject.getSelectedFile().Trim();
            
            if(fileName == "")
            {
                MessageBox.Show("请先选择记录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            dbSync syncDB = new dbSync();
            DataTable dt = new DataTable();
            dt = syncDB.getByFileName(fileName);
            if(dt.Rows.Count == 0)
            {
                MessageBox.Show("记录无法找到", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            _title =  "创建人:" + dt.Rows[0]["CREATEMAN"] + ", 创建日期:" + dt.Rows[0]["CREATEDATE"] + ",进度:";
            _fileStatus = int.Parse(dt.Rows[0]["FILESTATUS"].ToString());
            if(_fileStatus == 1)
            {
                _title += "出库";
            }
            else if (_fileStatus == 2)
            {
                _title += "清点一";
            }
            else if (_fileStatus == 3)
            {
                _title += "清点二";
            }
            else if (_fileStatus == 4)
            {
                _title += "入库";
            }
            else if (_fileStatus == 5)
            {
                _title += "已完成";
            }

            this.Text = _title;
            if(_fileStatus == 5)
            {
                btnSetIn.Enabled = false;
            }


            dt = syncDB.getSyncDataByName(fileName);

            dgvObject.setDgv(dgvSyncData);
            dgvObject.setDataSource(dt);
            dgvObject.setFormatComm();
            dgvObject.setFormatSyncData();
        }

        private void dgvSyncData_DataSourceChanged(object sender, EventArgs e)
        {
            dgvObject.setSyncDataRowFormat();
        }

        private void btnSetIn_Click(object sender, EventArgs e)
        {
            frmSetIN frmsetIN = new frmSetIN();
            frmsetIN.ShowDialog(this);
        }

        private void btnPrintSyncData_Click(object sender, EventArgs e)
        {
            DataTable rDt = new DataTable();
            //rDt.Columns.Add("标签ID", Type.GetType("System.String"));
            rDt.Columns.Add("名称", Type.GetType("System.String"));
            rDt.Columns.Add("编码", Type.GetType("System.String"));
            rDt.Columns.Add("出库人", Type.GetType("System.String"));
            rDt.Columns.Add("出库时间", Type.GetType("System.String"));
            rDt.Columns.Add("清点一人", Type.GetType("System.String"));
            rDt.Columns.Add("清点一时间", Type.GetType("System.String"));
            rDt.Columns.Add("清点二人", Type.GetType("System.String"));
            rDt.Columns.Add("清点二时间", Type.GetType("System.String"));
            rDt.Columns.Add("入库人", Type.GetType("System.String"));
            rDt.Columns.Add("入库时间", Type.GetType("System.String"));
            rDt.Columns.Add("备注", Type.GetType("System.String"));
            if (dgvSyncData.Rows.Count > 0)
            {
                for (int i = 0; i < dgvSyncData.Rows.Count; i++)
                {
                    DataRow dr = rDt.NewRow();
                    //dr["标签ID"] = dgvSyncData.Rows[i].Cells[0].Value.ToString();
                    dr["名称"] = dgvSyncData.Rows[i].Cells[1].Value.ToString();
                    dr["编码"] = dgvSyncData.Rows[i].Cells[2].Value.ToString();
                    dr["出库人"] = dgvSyncData.Rows[i].Cells[3].Value.ToString();
                    dr["出库时间"] = dgvSyncData.Rows[i].Cells[4].Value.ToString();
                    dr["清点一人"] = dgvSyncData.Rows[i].Cells[5].Value.ToString();
                    dr["清点一时间"] = dgvSyncData.Rows[i].Cells[6].Value.ToString();
                    dr["清点二人"] = dgvSyncData.Rows[i].Cells[7].Value.ToString();
                    dr["清点二时间"] = dgvSyncData.Rows[i].Cells[8].Value.ToString();
                    dr["入库人"] = dgvSyncData.Rows[i].Cells[9].Value.ToString();
                    dr["入库时间"] = dgvSyncData.Rows[i].Cells[10].Value.ToString();
                    dr["备注"] = dgvSyncData.Rows[i].Cells[11].Value.ToString();
                    rDt.Rows.Add(dr);
                }
            }

            dgvPrint.DataSource = rDt;

            DGVPrinter printer = new DGVPrinter();

            printer.TitleFont = new Font("宋体", 12, FontStyle.Bold);

            printer.Title = _title;
            printer.SubTitle = "------------------";
            printer.SubTitleFormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            printer.PageNumbers = true;
            printer.ShowTotalPageNumber = true;
            printer.PageNumberInHeader = false;
            printer.PorportionalColumns = true;
            printer.HeaderCellAlignment = StringAlignment.Near;
            printer.Footer = "";
            printer.FooterSpacing = 15;
            printer.PageSeparator = " / ";
            printer.PageText = "页";

            printer.PrintPreviewDataGridView(dgvPrint);
        }
    }
}
